package com.irdstudio.efp.flow.api.rest;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.irdstudio.basic.framework.core.constant.ApplicationConstance;
import com.irdstudio.basic.framework.core.constant.BaseConstant;
import com.irdstudio.basic.framework.core.constant.DataRuleType;
import com.irdstudio.basic.framework.core.constant.ResponseData;
import com.irdstudio.basic.framework.core.util.StringUtil;
import com.irdstudio.basic.framework.core.util.TimeUtil;
import com.irdstudio.basic.framework.core.vo.EngineHttpRequestVO;
import com.irdstudio.basic.framework.web.util.HttpClientUtil;
import com.irdstudio.basic.sequence.service.facade.SequenceService;
import com.irdstudio.efp.console.common.ConsoleConstant;
import com.irdstudio.efp.console.service.facade.PubSysInfoService;
import com.irdstudio.efp.flow.common.constance.FlowConstance;
import com.irdstudio.efp.flow.executor.util.AbstractController;
import com.irdstudio.efp.flow.service.FlowCommonService;
import com.irdstudio.efp.flow.service.FlowRepulseUtil;
import com.irdstudio.efp.flow.service.facade.BizFlowInfoService;
import com.irdstudio.efp.flow.service.facade.FlowUrlConfigService;
import com.irdstudio.efp.flow.service.vo.BizFlowInfoVO;
import com.irdstudio.efp.limit.service.facade.LmtPrdContService;
import com.irdstudio.efp.limit.service.vo.LmtPrdContVO;
import com.irdstudio.efp.loan.service.facade.AccLoanService;
import com.irdstudio.efp.loan.service.vo.AccLoanVO;
import com.irdstudio.efp.riskm.common.ExcelException;
import com.irdstudio.efp.riskm.common.util.ExcelImportUtil;
import com.irdstudio.efp.riskm.service.facade.LmtFreezeAppService;
import com.irdstudio.efp.riskm.service.facade.LmtUnfreezeAppService;
import com.irdstudio.efp.riskm.service.vo.LmtFreezeAppVO;
import com.irdstudio.efp.riskm.service.vo.LmtUnfreezeAppVO;
import com.irdstudio.efp.riskm.service.vo.PspSpecCheckCusVO;
import com.irdstudio.efp.riskm.service.vo.excelVo.LmtUnfreezeAppExcelVO;
import com.irdstudio.efp.riskm.service.vo.excelVo.PspSpecCheckCusExcelVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.charset.Charset;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api")
public class LmtUnfreezeAppController extends AbstractController {

    @Autowired
    @Qualifier("dbSeqService")
    private SequenceService sequenceService;

    @Autowired
    @Qualifier("lmtUnfreezeAppService")
    private LmtUnfreezeAppService lmtUnfreezeAppService;

    @Autowired
    @Qualifier("accLoanService")
    private AccLoanService accLoanService;
    @Autowired
    @Qualifier("bizFlowInfoService")
    private BizFlowInfoService bizFlowInfoService;

    @Autowired
    @Qualifier("pubSysInfoService")
    private PubSysInfoService pubSysInfoService;

    @Autowired
    @Qualifier("flowUrlConfigService")
    private FlowUrlConfigService flowUrlConfigService;

    @Autowired
    @Qualifier("lmtFreezeAppService")
    private LmtFreezeAppService lmtFreezeAppService;

    @Autowired
    private FlowCommonService flowCommonService;

    @Autowired
    @Qualifier("flowRepulseUtil")
    private FlowRepulseUtil flowRepulseUtil;

    /**
     * 列表数据查询
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value = "/lmt/unfreeze/apps", method = RequestMethod.POST)
    public @ResponseBody
    ResponseData<List<LmtUnfreezeAppVO>> queryLmtUnfreezeAppAll(@RequestBody LmtUnfreezeAppVO vo) {
        List<LmtUnfreezeAppVO> outputVo = null;
        // 根据用户角色获取用户权限方案
        //Map<String, String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
        //vo.setPrdCodebyBaseInfo(dataRuleTypeMap == null ? null : dataRuleTypeMap.get(PubConstant.PRD_CODE));
        DataRuleType dataRuleType = DataRuleType.getDataRuleType("01");
        setUserInfoToVO(vo);
        // 根据权限方案调用不同的方案进行数据查询
        switch (dataRuleType) {
            case Owner:
                outputVo = lmtUnfreezeAppService.queryAllOwner(vo);
                break;
            case CurrOrg:
                outputVo = lmtUnfreezeAppService.queryAllCurrOrg(vo);
                break;
            case CurrDownOrg:
                outputVo = lmtUnfreezeAppService.queryAllCurrDownOrg(vo);
                break;
            default:
                outputVo = lmtUnfreezeAppService.queryAllOwner(vo);
                break;
        }
        flowCommonService.displayOrgName(outputVo, "orgCode", "orgName");
        flowCommonService.displayUserName(outputVo, "cusManager", "cusManagerName");
        flowCommonService.displayUserName(outputVo, "lastUpdateUser", "lastUpdateUserName");
        flowCommonService.displayUserName(outputVo, "createUser", "createUserName");
        return getResponseData(outputVo);

    }

    /**
     * 根据主键查询详情
     * @return
     */
    @RequestMapping(value="/lmt/unfreeze/app", method=RequestMethod.GET)
    public @ResponseBody ResponseData<LmtUnfreezeAppVO> queryByPk() {
        LmtUnfreezeAppVO inVo = new LmtUnfreezeAppVO();
        LmtUnfreezeAppVO outputVo = lmtUnfreezeAppService.queryByPk(inVo);
        return getResponseData(outputVo);

    }

    /**
     * 根据主键删除信息
     * @param inLmtUnfreezeAppVo
     * @return
     */
    @RequestMapping(value="/lmt/unfreeze/app", method=RequestMethod.DELETE)
    public @ResponseBody ResponseData<Integer> deleteByPk(@RequestBody LmtUnfreezeAppVO inLmtUnfreezeAppVo) {
        int outputVo = lmtUnfreezeAppService.deleteByPk(inLmtUnfreezeAppVo);
        return getResponseData(outputVo);

    }

    /**
     * 根据主键更新信息
     * @param inLmtUnfreezeAppVo
     * @return
     */
    @RequestMapping(value = "/lmt/unfreeze/app", method = RequestMethod.PUT)
    public @ResponseBody ResponseData<Integer> updateByPk(@RequestBody LmtUnfreezeAppVO inLmtUnfreezeAppVo){
        setUserInfoToVO(inLmtUnfreezeAppVo);
        inLmtUnfreezeAppVo.setApplyDate(TimeUtil.getCurrentDate());
        inLmtUnfreezeAppVo.setLastUpdateTime(TimeUtil.getCurrentDateTime());
        inLmtUnfreezeAppVo.setLastUpdateUser(inLmtUnfreezeAppVo.getLoginUserId());
        inLmtUnfreezeAppVo.setAprvComment("");
        inLmtUnfreezeAppVo.setAprvUserCode("");
        inLmtUnfreezeAppVo.setAprvDate("");
        int outputVo=lmtUnfreezeAppService.updateByPk(inLmtUnfreezeAppVo);
        return getResponseData(outputVo);
    }

    /**
     * 获取审批通过的额度冻结信息
     * @param vo
     * @return
     */
    @RequestMapping(value="/get/lmt/freeze/apps", method= RequestMethod.POST)
    public @ResponseBody ResponseData<List<LmtFreezeAppVO>> queryLmtfreezeAppAll(@RequestBody LmtFreezeAppVO vo) {
        vo.setApproveStatus(ConsoleConstant.APRV_STATUS_03);
        List<LmtFreezeAppVO> output = lmtFreezeAppService.queryAllByConditions(vo);
        return getResponseData(output);
    }

    /**
     * 新增数据
     * @param inLmtUnfreezeAppVo
     * @return
     */
    @RequestMapping(value="/lmt/unfreeze/app", method=RequestMethod.POST)
    public @ResponseBody ResponseData<Integer> insertLmtUnfreezeApp(@RequestBody LmtUnfreezeAppVO inLmtUnfreezeAppVO) {
        int outputVo = 0;
        List<LmtUnfreezeAppVO> qsoutputVo = lmtUnfreezeAppService.queryByLmtContNoAll(inLmtUnfreezeAppVO);
        if (qsoutputVo.size()>0){
            for (LmtUnfreezeAppVO item : qsoutputVo) {
                if (ConsoleConstant.APRV_STATUS_01.equals(item.getApproveStatus()) || ConsoleConstant.APRV_STATUS_02.equals(item.getApproveStatus())  || ConsoleConstant.APRV_STATUS_05.equals(item.getApproveStatus())) {
                    outputVo = -1;
                    return getResponseData(outputVo);
                }
            }
        }
        setUserInfoToVO(inLmtUnfreezeAppVO);
        // 归属机构= 当前登陆人所在机构号
        inLmtUnfreezeAppVO.setOrgCode(inLmtUnfreezeAppVO.getLoginUserOrgCode());
        // 最后修改人= 当前登陆用户编号
        inLmtUnfreezeAppVO.setLastUpdateUser(inLmtUnfreezeAppVO.getLoginUserId());
        // 创建时间= 当前时间
        inLmtUnfreezeAppVO.setCreateTime(TimeUtil.getCurrentDateTime());
        // 申请时间= 当前时间
        inLmtUnfreezeAppVO.setApplyDate(TimeUtil.getCurrentDate());
        // 最后更新时间= 当前时间
        inLmtUnfreezeAppVO.setLastUpdateTime(TimeUtil.getCurrentDateTime());
        // 审批状态 = 待发起
        inLmtUnfreezeAppVO.setApproveStatus(ConsoleConstant.APRV_STATUS_01);
        inLmtUnfreezeAppVO.setAprvComment("");
        inLmtUnfreezeAppVO.setAprvUserCode("");
        inLmtUnfreezeAppVO.setAprvDate("");
        try {
            // 解冻流水号
            String uSeq = inLmtUnfreezeAppVO.getUnfrzSerno();
            if (StringUtil.isNullorBank(uSeq)){
                uSeq = sequenceService.getSequence("UNFRZ_SERNO","null","null");
            }
            inLmtUnfreezeAppVO.setUnfrzSerno(uSeq);
            String fSeq = lmtFreezeAppService.queryFrzSerno(inLmtUnfreezeAppVO.getLmtContNo());
            inLmtUnfreezeAppVO.setFrzSerno(fSeq);
            outputVo = lmtUnfreezeAppService.insertLmtUnfreezeApp(inLmtUnfreezeAppVO);

        } catch (Exception e) {
            logger.error("********************************************插入数据出错，异常信息为：", e.getMessage());
            outputVo = -1;
        }
        return getResponseData(outputVo);

    }


    /**
     * 查询数据 贷款台账表
     *
     * @param lmtUnfreezeAppVO
     * @return
     */
    @RequestMapping(value="/lmt/unfreeze/loan", method=RequestMethod.POST)
    public @ResponseBody
    ResponseData<List<AccLoanVO>> queryAccLoanAll(@RequestBody LmtUnfreezeAppVO lmtUnfreezeAppVO) {
        List<AccLoanVO> outputVo = null;
        AccLoanVO accLoanVO = new AccLoanVO();
        String certType = lmtUnfreezeAppVO.getCertType();
        String certCode = lmtUnfreezeAppVO.getCertCode();
        accLoanVO.setCertCode(certCode);
        accLoanVO.setCertType(certType);
        accLoanVO.setPage(lmtUnfreezeAppVO.getPage());
        accLoanVO.setSize(lmtUnfreezeAppVO.getSize());
        accLoanVO.setTotal(lmtUnfreezeAppVO.getTotal());
        outputVo = accLoanService.queryAccLoan(accLoanVO);
        return getResponseData(outputVo);
    }

    /**
     * 功能描述：提交数据
     * @param inLmtUnfreezeAppVO
     * @return
     */

    @RequestMapping(value = "/lmt/unfreeze/app/{action}", method = RequestMethod.POST)
    public @ResponseBody
    ResponseData<Integer> submit(@RequestBody LmtUnfreezeAppVO inLmtUnfreezeAppVO, @PathVariable("action") String action) {
        int outputVo=0;
        setUserInfoToVO(inLmtUnfreezeAppVO);
        if (action.equals("commit")) {
            insertLmtUnfreezeApp(inLmtUnfreezeAppVO);
        } else if (action.equals("save")) {
            updateByPk(inLmtUnfreezeAppVO);
        }
        if(inLmtUnfreezeAppVO.getApproveStatus().equals(ConsoleConstant.APRV_STATUS_05)){
            flowRepulseUtil.cleanRepulseAllInfo(inLmtUnfreezeAppVO.getUnfrzSerno());
        }
//        发起流程
        logger.info("即将发起额度解冻审批流程，信息为:" + JSON.toJSONString(inLmtUnfreezeAppVO));
        // 事件ID
        String eventId = "mLmtUnfreezeAppEnter";
        // 获取流程ID
        String flowId = "FW20190924000000001";
        inLmtUnfreezeAppVO.setApproveStatus(ConsoleConstant.APRV_STATUS_02);

        BizFlowInfoVO bizFlowInfoVO = new BizFlowInfoVO();
        bizFlowInfoVO.setBizFlowType(FlowConstance.BIZ_FLOW_TYPE_07);
        bizFlowInfoVO.setBizFlowId(flowId);
        List<BizFlowInfoVO> listFlow = bizFlowInfoService.queryBizFlowInfoByFlowType(bizFlowInfoVO);

        // 获取流程ID
        logger.info("查询到的业务流程参数为："+ JSONObject.toJSONString(listFlow));
        if (Objects.nonNull(listFlow) && listFlow.size()>0) {
            bizFlowInfoVO = listFlow.get(0);
            flowId = bizFlowInfoVO.getBizFlowId();  // 得到业务流转ID
        } else {
            outputVo = -1;
            return setResponseMessage(outputVo, "未配置相关信息提交失败!");
        }
        EngineHttpRequestVO eVo = new EngineHttpRequestVO();
        // 业务法人机构
        eVo.setBizLagelOrgCode(this.getUserInfo().getOrgInfo().getOrgCode());
        // 业务流水号
        eVo.setE_bizSerno(inLmtUnfreezeAppVO.getUnfrzSerno());
        eVo.setFlowId(flowId);
        //事件ID
        eVo.setEventId(eventId);
        eVo.setData(inLmtUnfreezeAppVO);
        eVo.setG_bizSerno(inLmtUnfreezeAppVO.getUnfrzSerno());
        if (!flowCommonService.dutyFlowCheck(eventId,flowId)){
            outputVo = -1;
            return setResponseMessage(outputVo, "发起审批流程失败!原因: 流程岗位已失效");
        }
        try {
            HttpClientUtil.callFlowEngine(flowUrlConfigService.getRestfulSyncStartEventUrl(), eVo, String.class);
            //更新 申请日期
            inLmtUnfreezeAppVO.setApplyDate(pubSysInfoService.getOpenday(BaseConstant.SYS_ID_1001));
            outputVo = lmtUnfreezeAppService.updateByPk(inLmtUnfreezeAppVO);
        } catch (Exception e) {
            // 发起失败
            e.printStackTrace();
            logger.error("LmtUnfreezeAppVO", e.getMessage());
            outputVo = -1;
            return setResponseMessage(outputVo, "发起额度解冻申请审批流程失败!原因: " + e.getMessage());
        }
        return getResponseData(outputVo);
    }

    private ResponseData<Integer> setResponseMessage(Integer outputVo, String message) {
        ResponseData<Integer> responseData = new ResponseData<Integer>();
        responseData.setMessage(message);
        responseData.setCode(ApplicationConstance.FAILURE);
        responseData.setSuccess(true);
        return responseData;
    }

    /**
     * 额度解冻申请信息模板下载
     * @author xiezw
     * @params  * @param null
     * @return
     * @date 2020/03/30
     * @version 0.1
     */
    private static final String CONTENTTYPE = "application/json;charset=UTF-8";

    private static final String MODELNAME = "额度解冻申请信息模板.xls";

    private static final String SHEETNAME = "额度解冻申请信息";

    private static final String IMPORT_PATH = "template/lmtUnfreezeAppTemp.xls";


    @RequestMapping(value = "/lmt/unfreeze/app/downLoad", method = RequestMethod.GET)
    public @ResponseBody ResponseData<String> cusRelCherkModelDownLoad(HttpServletResponse response, HttpServletRequest request) throws IOException {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            response.setContentType(CONTENTTYPE);
            response.setHeader("Content-Disposition", "attachment;filename=" + new String((MODELNAME).getBytes("GBK"), "ISO8859-1"));
            ServletOutputStream out = response.getOutputStream();
            logger.info("下载额度解冻数据导入模板开始----------------------------");
            bis = new BufferedInputStream(LmtUnfreezeAppController.class.getClassLoader().getResourceAsStream(IMPORT_PATH));
            bos = new BufferedOutputStream(out);
            byte[] buff = new byte[2048];
            int bytesRead;
            while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
                bos.write(buff, 0, bytesRead);
            }
        } catch (Exception e) {
            logger.error("额度解冻模板文件下载失败：", e);
            return getResponseData(null);
        } finally {
            if (null != bis) {
                bis.close();
            }
            if (null != bos) {
                bos.close();
            }
        }
        logger.info("下载额度解冻数据导入模板结束。。。。。。。。。。。。。。。。。。。");
        return getResponseData("success!");
    }

    @RequestMapping(value = "/lmt/unfreeze/app/upLoad", method = RequestMethod.POST)
    public void colltResRecordMapUpload(HttpServletResponse respone, HttpServletRequest request) {
        ResponseData<Integer> result = new ResponseData<>();
        List<File> cusRelCherkMapChecks = new ArrayList<File>();
        respone.setContentType(CONTENTTYPE);
        OutputStream out = null;
        int output = 0;
        try {
            out = respone.getOutputStream();
            //获取文件
            cusRelCherkMapChecks = flowCommonService.getFileList(request);
            if (!cusRelCherkMapChecks.isEmpty()) {
                for (File file : cusRelCherkMapChecks) {
                    List<LmtUnfreezeAppExcelVO> lmtUnfreezeAppExcelVOList = ExcelImportUtil.importExcelByClass(file, LmtUnfreezeAppExcelVO.class, SHEETNAME);
                    //检校数据
                    String errStr = "";
                    errStr = checkDatas(lmtUnfreezeAppExcelVOList);
                    //错误信息
                    if (!StringUtil.isNullorBank(errStr)) {
                        errStr = "ExcelError:" + errStr;
                        throw new Exception(errStr);
                    }
                    //组装数据插入数据
                    output = insertTransform(lmtUnfreezeAppExcelVOList);
                    if (output<0){
                        result = result.createData(output, ApplicationConstance.FAILURE,"插入数据失败！", false);
                    }
                    //组装结果
                    result = result.createData(output, ApplicationConstance.SUCCESS,"上传文件成功！", true);
                }
            } else {
                throw new ExcelException("获取不到上传的文件！");
            }
        } catch (Exception e) {
            logger.error("文件上传失败,失败原因:", e);
            result = result.createData(output, ApplicationConstance.FAILURE, e.getMessage(), false);
        } finally {
            String str = JSON.toJSONString(result);
            try {
                out.write(str.getBytes(Charset.forName("UTF-8")));
                out.flush();
                out.close();
            } catch (IOException e) {
                logger.error("关闭数据流OutputStream出错，错误信息为：", e);
            }
        }
    }

    private String checkDatas(List<LmtUnfreezeAppExcelVO> list) {
        String rs = "";
        LmtFreezeAppVO lmtFreezeAppVO = new LmtFreezeAppVO();
        for (LmtUnfreezeAppExcelVO item : list){
            // 必填项校验
            if (StringUtil.isNullorBank(item.getFrzSerno())){
                return "导入数据冻结流水号为空";
            }
            if (StringUtil.isNullorBank(item.getLmtContNo())){
                return "导入数据授信协议编号为空";
            }
            if (StringUtil.isNullorBank(item.getCusId())){
                return "导入数据客户编号为空";
            }
            if (StringUtil.isNullorBank(item.getCusName())){
                return "导入数据客户名称为空";
            }
            if (StringUtil.isNullorBank(item.getCertType())){
                return "导入数据证件类型为空";
            }
            if (StringUtil.isNullorBank(item.getCertCode())){
                return "导入数据证件号码为空";
            }
            if (StringUtil.isNullorBank(item.getLmtAmt().toString())){
                return "导入数据授信额度为空";
            }
            if (StringUtil.isNullorBank(item.getAvailAmt().toString())){
                return "导入数据可用额度为空";
            }
            if (StringUtil.isNullorBank(item.getChannelNo())){
                return "导入数据渠道号为空";
            }
            if (StringUtil.isNullorBank(item.getPrdId())){
                return "导入数据产品ID为空";
            }
            if (StringUtil.isNullorBank(item.getPrdCode())){
                return "导入数据产品编号为空";
            }
            if (StringUtil.isNullorBank(item.getPrdName())){
                return "导入数据产品名称为空";
            }
            if (StringUtil.isNullorBank(item.getCusManager())){
                return "导入数据客户经理为空";
            }
            if (StringUtil.isNullorBank(item.getUnfrzReason())){
                return "导入数据的解冻原因为空";
            }
            if (StringUtil.isNullorBank(item.getCyclicFlg())){
                return "导入数据是否循环必输项为空";
            }
            // 数据是否有效校验
            String rss = "冻结流水号【 " + item.getFrzSerno() + " 】，";
            lmtFreezeAppVO.setFrzSerno(item.getFrzSerno());
            LmtFreezeAppVO queryLmtFreezeAppVO = lmtFreezeAppService.queryByPk(lmtFreezeAppVO);
            if (null == queryLmtFreezeAppVO){
                return rss + "导入数据的冻结流水号有误";
            }
            if (!ConsoleConstant.APRV_STATUS_03.equals(queryLmtFreezeAppVO.getApproveStatus())){
                return rss + "导入数据的存在未完成或已被拒绝的冻结操作";
            }
            if (!queryLmtFreezeAppVO.getLmtContNo().equals(item.getLmtContNo())){
                return rss + "导入数据授信协议编号有误！";
            }
            if (!queryLmtFreezeAppVO.getCertType().equals(item.getCertType())){
                return rss + "导入数据证件类型有误！";
            }
            if (!queryLmtFreezeAppVO.getCertCode().equals(item.getCertCode())){
                return rss + "导入数据证件号码有误！";
            }
            if (!queryLmtFreezeAppVO.getCusId().equals(item.getCusId())){
                return rss + "导入数据客户编号有误";
            }
            if (!queryLmtFreezeAppVO.getCusName().equals(item.getCusName())){
                return rss + "导入数据客户名称有误";
            }
            if (!queryLmtFreezeAppVO.getChannelNo().equals(item.getChannelNo())){
                return rss + "导入数据渠道号有误";
            }
            if (!queryLmtFreezeAppVO.getPrdId().equals(item.getPrdId())){
                return rss + "导入数据产品ID有误";
            }
            if (!queryLmtFreezeAppVO.getPrdCode().equals(item.getPrdCode())){
                return rss + "导入数据产品编号有误";
            }
            if (!queryLmtFreezeAppVO.getPrdName().equals(item.getPrdName())){
                return rss + "导入数据产品名称有误";
            }
            if (!queryLmtFreezeAppVO.getCusManager().equals(item.getCusManager())){
                return rss + "导入数据客户经理有误";
            }
            if (!queryLmtFreezeAppVO.getCyclicFlg().equals(item.getCyclicFlg())){
                return rss + "导入数据是否循环有误";
            }
            String as =  "授信协议【 " + item.getLmtContNo() + " 】，";
            LmtPrdContVO lmtPrdContInVO = new LmtPrdContVO();
            lmtPrdContInVO.setLmtContNo(item.getLmtContNo());
            lmtPrdContInVO.setLmtStatus("00");
            LmtPrdContVO queryLmtPrdContVO = lmtPrdContService.queryByPk(lmtPrdContInVO);
            if (Objects.isNull(queryLmtPrdContVO)){
                return as + "未找到冻结的额度信息";
            }
            if (!(queryLmtPrdContVO.getLmtAmt().compareTo(item.getLmtAmt()) == 0)){
                return as + "导入数据授信额度有误";
            }
            if (!(queryLmtPrdContVO.getAvailAmt().compareTo(item.getAvailAmt()) == 0)){
                return as + "导入数据可用额度有误";
            }

            // 数据是否存在未完成的额度解冻操作
            LmtUnfreezeAppVO lmtUnfreezeAppVO = new LmtUnfreezeAppVO();
            lmtUnfreezeAppVO.setLmtContNo(item.getLmtContNo());
            List<LmtUnfreezeAppVO> qsoutputVo = lmtUnfreezeAppService.queryByLmtContNoAll(lmtUnfreezeAppVO);
            if (qsoutputVo.size()>0){
                for (LmtUnfreezeAppVO item2 : qsoutputVo) {
                    if (ConsoleConstant.APRV_STATUS_01.equals(item2.getApproveStatus()) || ConsoleConstant.APRV_STATUS_02.equals(item2.getApproveStatus())  || ConsoleConstant.APRV_STATUS_05.equals(item2.getApproveStatus())) {
                        return "授信额度协议编号【"+ item.getLmtContNo() +"】存在未完成的额度解冻操作";
                    }
                }
            }
        }
        return rs;
    }

    private int insertTransform(List<LmtUnfreezeAppExcelVO> list){
        int output = 0;
        setUserInfoToVO(list.get(0));
        String userId = list.get(0).getLoginUserId();
        String userOrgId = list.get(0).getLoginUserOrgCode();
        String currentTime = TimeUtil.getCurrentDateTime();
        LmtUnfreezeAppVO lmtUnfreezeAppVO = new LmtUnfreezeAppVO();
        try {
            for (LmtUnfreezeAppExcelVO item : list){
                String uSeq = sequenceService.getSequence("UNFRZ_SERNO","null","null");
                lmtUnfreezeAppVO.setUnfrzSerno(uSeq);
                lmtUnfreezeAppVO.setFrzSerno(item.getFrzSerno());
                lmtUnfreezeAppVO.setLmtContNo(item.getLmtContNo());
                lmtUnfreezeAppVO.setCusId(item.getCusId());
                lmtUnfreezeAppVO.setCusName(item.getCusName());
                lmtUnfreezeAppVO.setCertType(item.getCertType());
                lmtUnfreezeAppVO.setCertCode(item.getCertCode());
                lmtUnfreezeAppVO.setLmtAmt(item.getLmtAmt());
                lmtUnfreezeAppVO.setAvailAmt(item.getAvailAmt());
                lmtUnfreezeAppVO.setChannelNo(item.getChannelNo());
                lmtUnfreezeAppVO.setPrdId(item.getPrdId());
                lmtUnfreezeAppVO.setPrdCode(item.getPrdCode());
                lmtUnfreezeAppVO.setPrdName(item.getPrdName());
                lmtUnfreezeAppVO.setCusManager(item.getCusManager());
                lmtUnfreezeAppVO.setUnfrzReason(item.getUnfrzReason());
                lmtUnfreezeAppVO.setCyclicFlg(item.getCyclicFlg());
                lmtUnfreezeAppVO.setRemarks(item.getRemarks());
                //创建修改时间、创建者、修改者
                lmtUnfreezeAppVO.setCreateUser(userId);
                lmtUnfreezeAppVO.setCreateTime(currentTime);
                lmtUnfreezeAppVO.setLastUpdateTime(currentTime);
                lmtUnfreezeAppVO.setLastUpdateUser(userId);
                // 归属机构
                lmtUnfreezeAppVO.setOrgCode(userOrgId);
                // 申请日期
                lmtUnfreezeAppVO.setApplyDate(TimeUtil.getCurrentDate());
                // 待发起
                lmtUnfreezeAppVO.setApproveStatus(ConsoleConstant.APRV_STATUS_01);
                output = lmtUnfreezeAppService.insertLmtUnfreezeApp(lmtUnfreezeAppVO);
            }
        } catch (Exception e) {
            e.printStackTrace();
            output = -1;
        }finally {
            return output;
        }
    }

    @Autowired
    @Qualifier("lmtPrdContService")
    private LmtPrdContService lmtPrdContService;
}
